import sys
import os

# 将当前 Python 文件所在目录添加到 sys.path，确保 Python 解释器可以找到该目录中的模块，从而支持 import 语句
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
print(sys.path)

from ai_quickstart.openai_translator.ai_translator.model import OpenAIModel
from ai_quickstart.openai_translator.ai_translator.translator.pdf_translator import PDFTranslator
from ai_quickstart.openai_translator.ai_translator.utils import ArgumentParser, ConfigLoader

if __name__ == '__main__':
    argument_parser = ArgumentParser()
    args = argument_parser.parse_arguments()
    config_loader = ConfigLoader(args.config)

    # 读取配置文件
    config = config_loader.load_config()

    # 从参数，或配置文件中获取执行所需的参数
    model_name = args.openai_model if args.openai_model else config['OpenAIModel']['model']
    api_key = args.openai_api_key if args.openai_api_key else config['OpenAIModel']['api_key']
    base_url = args.openai_base_url if args.openai_base_url else config['OpenAIModel']['base_url']
    # 封装OpenAI客户端
    model = OpenAIModel(model=model_name, api_key=api_key, base_url=base_url)

    pdf_file_path = args.book if args.book else config['common']['book']
    file_format = args.file_format if args.file_format else config['common']['file_format']

    # 实例化 PDFTranslator 类，并调用 translate_pdf() 方法
    translator = PDFTranslator(model)
    translator.translate_pdf(pdf_file_path, file_format)
